package cn.buk.example.limits_service;

import cn.buk.example.limits_service.bean.LimitConfiguration;
import com.sun.net.httpserver.Authenticator;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.bind.annotation.GetMapping;

@RestController
public class LimitsConfigurationController {

    @Autowired
    private Configuration configuration;

    private static final String SERVICE_NAME = "limits-service";

    @GetMapping("/limits")
    @CircuitBreaker(name = SERVICE_NAME, fallbackMethod = "fallbackMethod")
    public LimitConfiguration retrieveLimitsFromConfigurations() {
        return new LimitConfiguration(configuration.getMaximum(), configuration.getMinimum());
    }

    private String fallbackMethod(Exception e) {
        return "{'err_msg': 'Address service is not responding properly'}";
    }
}
